package com.tzy.note.base.thread.sync;

import java.util.ArrayList;
import java.util.List;

/**
 * 线程不安全的集合：List
 */
public class UnsafeList {
    public static void main(String[] args) throws InterruptedException {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 10000; i++) {
            new Thread(()->{
                synchronized (list){
                    list.add(Thread.currentThread().getName());
                }
            }).start();
        }
        Thread.sleep(5000);
        //多个线程在同一瞬间，操作同一位置块，多个线添加到同一位置，把list元素小于总数
        System.out.println(list.size());
    }
}

